unit u_chatform;

interface

uses
    Microsoft.DirectX,
    Microsoft.DirectX.Direct3D,
    sysutils,
    windows,
    u_asus,
    Microsoft.DirectX.Direct3DX,
    system.drawing,
    u_font;

type

  c_chat = class

    private

      u_sprite : Sprite;
      t_fond : Texture;
      t_croixh : Texture;
      t_croixb : Texture;
      t_envoyer : Texture;
      t_shoutoff : Texture;
      t_shouton : Texture;
      trait: texture;

      title : u_text;
      shout_text : u_text;
      envoi: u_text;
      text_to_shout : string;
      focus : string;

      show_envoi : integer;
      is_alive : boolean;



      chat_line : Array[0..7] of  u_text;


      is_on : boolean;


      t_cursor : Texture;
      c_x, c_y : integer;


    public
    is_active : boolean;
    reduite : boolean;
    textrec : string;
    wait : boolean;
    coordx, coordy : integer;

    constructor create(dev_ : Device; x, y : integer);
    procedure chat_render;
    procedure move_chat(varx, vary : integer);
    function is_clicked(mouse_x, mouse_y : integer) : boolean;
    function isalive : boolean;
    procedure show(bol : boolean);
    procedure add_text(msg_ : string);
    function int_to_char(key_: Integer) : char;
    function isactive : boolean;
    procedure on_key(b : integer);
    procedure show_cursor(x, y : integer);
    procedure setpos(m_x, m_y: integer);
    procedure affi_envoi;


  end;


implementation

uses u_engine;


//   modif
    constructor c_chat.create(dev_: Device; x, y : integer);
    var
      i : integer;
      begin
        inherited create;
        coordx := x;
        coordy := y;

        focus := '';
        text_to_shout := '';

        show_envoi := 0;

        u_sprite := sprite.Create(dev_);
        t_fond := Textureloader.FromFile(dev_, 'asus menu\chat_win\fond2.png');
        trait := Textureloader.FromFile(dev_, 'asus menu\chat_win\trait.png');
        t_croixh := Textureloader.FromFile(dev_, 'asus menu\chat_win\cross.png');
        t_croixb := Textureloader.FromFile(dev_, 'asus menu\chat_win\cross_bas.png');
        t_envoyer := Textureloader.FromFile(dev_, 'asus menu\chat_win\but_envoy_haut.png');
        t_shoutoff := Textureloader.FromFile(dev_, 'asus menu\chat_win\shoutbox_off.png');
        t_shouton := Textureloader.FromFile(dev_, 'asus menu\chat_win\shoutbox_on.png');
//        t_cursor := Textureloader.FromFile(dev_, 'asus menu\curseur.JPG');   

        for i := 0 to 7 do
          begin
          chat_line[i] := u_text.create(dev_, 8, color.black, 'hello : ' + inttostr(i), 'Comic Sans MS', FontStyle.Regular);
          chat_line[i].set_coord(coordx + 5, coordy + 28 + 9 * i);
          chat_line[i].set_visible(True);
          end;

        title := u_text.create(dev_, 12, color.Black, 'Le Chat des warriors', 'Comic Sans MS', FontStyle.Bold);
        title.set_coord(coordx + 5, coordy + 3);
        envoi := u_text.create(dev_, 8, color.Black, '', 'Comic Sans MS', FontStyle.Regular);
        shout_text := u_text.create(dev_, 8, Color.Black, '', 'Comic Sans MS', FontStyle.Regular);
        shout_text.set_coord(coordx + 7, coordy + 106);
        is_alive := false;
        is_active := true;
        reduite := true;
        is_on := false;
        textrec:='';              //
        wait:=false;
      end;

//modif

    procedure c_chat.chat_render;
    var
      i : integer;
      begin
      if is_alive then
        begin
          u_sprite.&Begin(spriteflags.AlphaBlend);
          if reduite then
            u_sprite.Draw(t_croixb, Vector3.Create(0, 0, 0), Vector3.Create(0, 0, 0), color.White.ToArgb)
            else
            begin
              u_sprite.Draw(t_fond, Vector3.Create(0, 0, 0), Vector3.Create(coordx, coordy, 0), color.White.ToArgb);
              u_sprite.Draw(trait, Vector3.Create(0, 0, 0), Vector3.Create(coordx, coordy + 24, 0), color.White.ToArgb);
              u_sprite.Draw(t_croixh, Vector3.Create(0, 0, 0), Vector3.Create(coordx + 235, coordy + 5, 0), color.White.ToArgb);
                u_sprite.Draw(t_envoyer, Vector3.Create(0, 0,0), Vector3.Create(coordx + 185, coordy + 105, 0), color.White.ToArgb);
              if focus = 'shout' then
                u_sprite.Draw(t_shouton, Vector3.Create(0, 0, 0), Vector3.Create(coordx + 5, coordy + 105, 0), Color.White.ToArgb)
              else
                u_sprite.Draw(t_shoutoff, Vector3.Create(0, 0, 0), Vector3.Create(coordx + 5, coordy + 105, 0), Color.White.ToArgb);
              if is_on then
                u_sprite.Draw(t_cursor, Vector3.Create(0, 0, 0), Vector3.Create(c_x, c_y, 0), Color.White.ToArgb);
              end;
          u_sprite.&End;
          if not reduite then
          begin
            for I := 0 to 7 do
              chat_line[i].text_render;
            title.text_render;
            shout_text.text_render;
          end;
          affi_envoi;
        end;
      end;

    procedure c_chat.add_text( msg_: string);          //
    var
      i,j,k : integer;
      last_pos, cur_pos : integer;
      buf : string;
      prout : string;
      kalimdor : string;
      count : integer;
      begin
        last_pos := 1;
        cur_pos := 1;
        kalimdor := msg_;
        count := 0;
        prout:='';
        buf:='';
        while cur_pos <= Length(kalimdor) do
          begin
          prout:='';
          buf:='';
            if (kalimdor[cur_pos] = #10) or (cur_pos = Length(kalimdor)) then
              begin
                if kalimdor[cur_pos] = #10 then
                  prout := Copy(kalimdor, last_pos, cur_pos - last_pos)
                  else
                  prout := Copy(kalimdor, last_pos, cur_pos - last_pos + 1);
                cur_pos := cur_pos + 1;
                last_pos := cur_pos;
//                count := count +1;
                for i := 1 to 7 do
                  begin
                    buf := chat_line[i].get_text;
                    chat_line[i - 1].change_text(buf);
                  end;

                if length(prout)>33 then
                  begin
                  i:=33;
                    while i >0  do
                      begin
                        if prout[i]=' ' then
                          begin
                            last_pos := last_pos - 1 - length(prout) + i ;
                            cur_pos := last_pos;
                            break;
                          end
                        else
                          i:=i-1;
                      end;
                  if i=0 then
                    i:=33;
                  setlength(prout,i);

                  end;

                chat_line[7].change_text(prout);

              end;
            cur_pos := cur_pos + 1;

          end;
        if myplayer.game_type='o' then
        begin
        if wait then              //
                  begin
                    if prout= 'continuer' then
                      wait:=false
                    else
                      if prout='arreter' then
                        dialoge.tabdial:=nil
                      else
                        textrec := prout;
//                    reprise(wait, textrec);
                  end;
        end;

      end;

    procedure c_chat.affi_envoi;
    begin
      if not reduite then
        begin
          envoi.set_coord(coordx + 192, coordy + 103);
          if show_envoi <> 0 then
            begin
              envoi.change_color(color.Red);
              show_envoi := show_envoi - 1;
            end;
          envoi.change_text('Envoyer');
          envoi.text_render;
          envoi.change_color(color.Black);
        end;
    end;

    procedure c_chat.move_chat(varx: Integer; vary: Integer);
    var
      i : integer;
      begin
        coordx := coordx + varx;
        coordy := coordy + vary;
        for i := 0 to 7 do
          chat_line[i].set_coord(coordx + 5, coordy + 28 + 9 * i);
        title.set_coord(coordx + 5, coordy + 3);
        shout_text.set_coord(coordx + 7, coordy + 106);
      end;

    procedure c_chat.Show_cursor(x, y : integer);
      begin
        c_x :=  x;
        c_y := y;
        if (x > coordx) and (x < coordx + 255) and
           (y > coordy) and (y < coordy + 125) then
              is_on := true
              else
              is_on := false;
      end;

    function c_chat.is_clicked(mouse_x: Integer; mouse_y: Integer) : boolean;
      begin
        if (mouse_x > 0) and (mouse_x < 15) and
            (mouse_y > 0) and (mouse_y < 15) then
            begin
              Result := true;
              reduite := false;
              is_active := true;
              Exit;
            end;

        if (mouse_x > coordx) and (mouse_x < coordx + 255) and
            (mouse_y > coordy) and (mouse_y < coordy + 125) then
            begin
              if (mouse_x > coordx + 235) and (mouse_x < coordx + 250) and
                 (mouse_y > coordy + 5) and (mouse_y < coordy + 20) then
                 begin
                  reduite := true;
                  is_active := false;
                  Result := true;
                  Exit;
                 end;
              if reduite then
              Result := false
              else
              result := true;

              if (mouse_x > coordx + 185) and (mouse_x < coordx + 248) and
                 (mouse_y > coordy + 106) and (mouse_y < coordy + 120) then
                 begin
                   //Rajouter l'envoi pour le resau!!
                   //**************************************//
                   Self.add_text(text_to_shout);
                   focus := 'none';
                   text_to_shout := '';
                   shout_text.change_text('');
                   show_envoi := 10;
                   Exit;
                 end;
               if (mouse_x > coordx + 5) and (mouse_x < coordx + 132) and
                 (mouse_y > coordy + 105) and (mouse_y < coordy + 120) then
                 begin
                  focus := 'shout';
                  Exit;
                 end;
               Exit;
            end
            else
              begin
              Result := false;
              is_active := False;
              focus := 'none';
              end;

      end;


    function c_chat.int_to_char(key_: Integer) : char;
    begin
     if key_ = 190 then
        Result := '.'
        else
        if GetKeyState(VK_CAPITAL) = 0 then
          begin
            if key_ in [32..126] then
              begin
                if key_ in [65..91] then
                  key_ := key_ + 32;
                Result := chr(key_)
              end;
          end
          else
          if key_ in [32..126] then
            Result := chr(key_)
            else
            Result := ']';
    end;

    procedure c_chat.on_key(b: Integer);
      begin
        if (is_active) then
          if (focus = 'shout') then
            begin
            if b = 13 then
              begin
                //Rajouter l'envoi pour le réseau
                //*************************************//
                myplayer.send_message(text_to_shout);
                text_to_shout := '';
                focus := 'none';
                shout_text.change_text('');
                Exit;
              end
              else if (b = 8) then
                begin
                  if (Length(text_to_shout) > 0) then
                    begin
                      //delete
                      SetLength(text_to_shout, length(text_to_shout) - 1);
                      shout_text.change_text(text_to_shout);
                    end;
                end
                else
                begin
                  text_to_shout := text_to_shout + int_to_char(b);
                  shout_text.change_text(text_to_shout);
                end;
              end
          else
          begin
            case b of
              37 : move_chat(-1, 0);
              38 : move_chat(0, -1);
              39 : move_chat(1, 0);
              40 : move_chat(0, 1);
            end;
          end;
      end;                                

    function c_chat.isalive;
      begin
        Result := is_alive;
      end;

    function c_chat.isactive : boolean;
      begin
        Result := is_active;
      end;

    procedure c_chat.show(bol: Boolean);
      begin
        is_alive := bol;
      end;

procedure c_chat.setpos(m_x: Integer; m_y: Integer);
var
  i: integer;
begin
  coordx := m_x;
  coordy := m_y;
  for i := 0 to 7 do
    chat_line[i].set_coord(coordx + 5, coordy + 28 + 9 * i);
  title.set_coord(coordx + 5, coordy + 3);
  shout_text.set_coord(coordx + 7, coordy + 106);
end;

end.
